<?php

/**
 * Framework_DB_DaoCollectionPager
 *
 * @author Application Support Group
 * @package BAMF
*/
class Framework_DB_DaoCollectionPager extends Framework_Object {

	protected $_attributes ;

	public function __construct( $limit=null, $currpage=null, $showMaxRecords=false ) {

		parent::__construct();
		
		if( empty( $limit )) { $limit=PAGER_RECORDS_PER_PAGE ; }
		if( empty( $currpage )) { $currpage=PAGER_DEFAULT_PAGE ; }
		
		$this->_attributes = array( 
			'limit'=>$limit,
			'currpage'=>$currpage, 
			'offset'=> $limit * ($currpage - 1), 
			'pagecount'=>1,
			'totalrecs'=>0,
			'showmaxrecords'=>$showMaxRecords 
			);
		
		if( $this->showmaxrecords ) {
			$this->offset = $this->totalrecs * ($this->currpage - 1) ;
		}

	}

	
	public function calculateOffset() {
		if( $this->showmaxrecords ) {
			$this->limit = $this->totalrecs ;
		}

		$this->offset = $this->limit * ($this->currpage - 1) ;
	}



	public function __get( $anAttribute ) {

		$_output = false ;

		if( isset( $this->_attributes[$anAttribute] )) {
			$_output = $this->_attributes[$anAttribute] ;
		} else {
			$_output = Framework_ErrorHandler::raiseError( 
				"Cannot get the requested property.", 
				__METHOD__."::".__LINE__ 
				) ;
		}

		return $_output ;
	}

	public function __set( $anAttribute, $aValue ) {

		$_output = false ;

		if( isset( $this->_attributes[$anAttribute] )) {
			$this->_attributes[$anAttribute] = $aValue ;
			$_output = true ;
		} else {
			$_output = Framework_ErrorHandler::raiseError( 
				"Cannot set the requested property.", 
				__METHOD__."::".__LINE__ 
				) ;
		}

		$this->calculateOffset() ;
		return $_output ;
	}


	public static function getMaxPager() {
		return new Framework_DB_DaoCollectionPager(null,null,true) ;
	}

	public function __destruct() {
		parent::__destruct();
	}



	public function __toString() {

		return __CLASS__;

	}
}

?>
